package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.DBTopic;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.Topic;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.comman.TopicContacts;
import com.yuntongxun.ecdemo.voicechat.db.DBManager;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import java.io.IOException;

import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 话题
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/22
 */

public class LittleQuizActivity extends BaseChatActivity implements ICommonViewUi {

    //答题中
    public static final int PROGRESSING=1;
    //正常状态
    public static final int NORMAL=0;

    public static final String CID="cid";

    int type=NORMAL;

    @InjectView(R.id.quiz_question_tv)
    TextView questionTv;
    @InjectView(R.id.quiz_answer_tv)
    TextView answerTv;
    @InjectView(R.id.quiz_next_btn)
    TextView nextBtn;
    @InjectView(R.id.relase_btn)
    Button relaseBtn;
    @InjectView(R.id.quiz_question_img)
    ImageView questionImg;

    DBManager dbManager;
    String cid="";

    private ICommonRequestPresenter iCommonRequestPresenter;

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_little_quiz;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Little Quiz");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        cid=getIntent().getStringExtra(CID);
        dbManager=new DBManager(this);
        DBTopic dbTopic=dbManager.getTopic(cid);
        if (dbTopic==null){
            type=NORMAL;
            nextBtn.setVisibility(View.VISIBLE);
            toRequest(ApiConstants.EventTags.TOPIC_TAKE);
        }else{
            type=PROGRESSING;
            nextBtn.setVisibility(View.GONE);
            String msg=dbTopic.getTopicInfo();
            JsonHelper<Topic> jsonHelper = new JsonHelper<Topic>(Topic.class);
            topic = jsonHelper.getData(msg, "result");
            bindData(topic);
            relaseBtn.setText("Release answer");
        }

    }


    @Override
    public void toRequest(int eventTag) {
        switch (eventTag){
            case ApiConstants.EventTags.TOPIC_TAKE:
                RequestParams map = new RequestParams();
                iCommonRequestPresenter.request(eventTag, mContext, ApiConstants.Urls.TOPIC_TAKE, map);
                break;
        }

    }
    Topic topic;
    String result="";
    @Override
    public void getRequestData(int eventTag, String result) {
        this.result=result;
        JsonHelper<Topic> jsonHelper = new JsonHelper<Topic>(Topic.class);
        topic = jsonHelper.getData(result, "result");
        bindData(topic);
    }

    private void bindData(Topic topic){
        if (topic==null){
            return;
        }
        if (topic.getType()==Topic.VOICE){
            questionImg.setVisibility(View.VISIBLE);
            questionTv.setVisibility(View.GONE);
        }else{
            questionImg.setVisibility(View.GONE);
            questionTv.setVisibility(View.VISIBLE);
            questionTv.setText(topic.getTopic());
        }

    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
    }

//    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
//        if (status) {
//            mPostingdialog = new ECProgressDialog(this, "Waiting");
//            mPostingdialog.show();
//        } else {
//            mPostingdialog.dismiss();
//        }
    }

    @OnClick(R.id.quiz_next_btn)
    public void onNextBtnClick(){
        toRequest(ApiConstants.EventTags.TOPIC_TAKE);
    }


    @OnClick(R.id.relase_btn)
    public void onRelaseBtnClick(){

        if(topic == null){
            showToastShort("请先获取话题");
            return;
        }

        String topicMessage="";

        if (type==NORMAL){
            topicMessage = TopicContacts.getTopicContact(topic.getTopic(), "", topic.getType());
            DBTopic dbTopic=new DBTopic();
            dbTopic.setCid(cid);
            dbTopic.setTopicInfo(result);
            dbTopic.setType(NORMAL);
            dbManager.addTopic(dbTopic);
        }else{
            topicMessage = TopicContacts.getTopicContact(topic.getTopic(), topic.getAnswer(), topic.getType());
            dbManager.deleteTopic(cid);
        }

        Log.e("result", topicMessage);

        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.TOPIC_KEY);
        postResult.setResult(topicMessage);

        EventBus.getDefault().post(postResult);

        finish();
    }


    MediaPlayer mediaPlayer;
    @OnClick(R.id.quiz_question_img)
    public void onQuestionBtn(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }
        mediaPlayer=new MediaPlayer();
        try {
            mediaPlayer
                    .setDataSource(topic.getTopic());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            Log.v("AUDIOHTTPPLAYER", e.getMessage());
        }
    }
}
